#include "lobby.hpp"

#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>
#include <QString>

Lobby::Lobby(QMainWindow * parent) :
    QWidget(parent)
{
    _listGames = new QTreeWidget(this);
    _listPlayers = new QListWidget(this);
    _listMessages = new QTextEdit(this);
    _message = new QLineEdit(this);
    _send = new QPushButton("Send", this);

    _dockPlayers = new QDockWidget("Players", parent);
    _dockChat = new QDockWidget("Chat", parent);
    _dockGames = new QDockWidget("Games", parent);

    _dockPlayersContent = new QWidget(parent);
    _dockChatContent = new QWidget(parent);
    _dockGamesContent = new QWidget(parent);

    _layoutPlayers = new QGridLayout(_dockPlayersContent);
    _layoutChat = new QGridLayout(_dockChatContent);
    _layoutGames = new QGridLayout(_dockGamesContent);

    _playersTitle = new QLabel("Players", parent);
    _chatTitle = new QLabel("Chat", parent);
    _gamesTitle = new QLabel("Games", parent);

    _dockPlayersContent->setLayout(_layoutPlayers);
    _dockChatContent->setLayout(_layoutChat);
    _dockGamesContent->setLayout(_layoutGames);

    _layoutPlayers->addWidget(_listPlayers);

    _layoutChat->addWidget(_listMessages, 0, 0, 1, 5);
    _layoutChat->addWidget(_message, 1, 0, 1, 4);
    _layoutChat->addWidget(_send, 1, 4);

    _layoutGames->addWidget(_listGames);

    _dockPlayers->setWidget(_dockPlayersContent);
    _dockChat->setWidget(_dockChatContent);
    _dockGames->setWidget(_dockGamesContent);

    _dockPlayers->setFeatures(QDockWidget::DockWidgetFloatable);
    _dockChat->setFeatures(QDockWidget::DockWidgetFloatable);
    _dockGames->setFeatures(QDockWidget::DockWidgetFloatable);

    parent->addDockWidget(Qt::RightDockWidgetArea, _dockPlayers);
    parent->addDockWidget(Qt::LeftDockWidgetArea, _dockGames);
    parent->addDockWidget(Qt::LeftDockWidgetArea, _dockChat);

    _dockPlayers->setTitleBarWidget(_playersTitle);
    _dockChat->setTitleBarWidget(_chatTitle);
    _dockGames->setTitleBarWidget(_gamesTitle);

    _listMessages->setReadOnly(true);

    _playersTitle->setContentsMargins(10, 5, 25, 0);
    _chatTitle->setContentsMargins(10, 5, 25, 0);
    _gamesTitle->setContentsMargins(10, 5, 25, 0);

    QStringList qsl;
    qsl.append("id");
    qsl.append("Goban");
    qsl.append("Komi");
    qsl.append("Black");
    qsl.append("White");

    _listGames->setHeaderLabels(qsl);
    _listGames->header()->setResizeMode(QHeaderView::ResizeToContents);
    _listGames->header()->setStretchLastSection(false);
    _listGames->header()->setMovable(false);
    _listGames->header()->setSectionHidden(0, true);
    _listGames->header()->setDefaultAlignment(Qt::AlignCenter);

    _dockPlayers->setMaximumWidth(150);

    connect(_send, SIGNAL(clicked()), this, SLOT(sendMessage()));

    connect(_listPlayers, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(kick(QListWidgetItem*)));
}

void Lobby::addClient(const QString & name)
{
    _listPlayerNames.append(name);

    _listPlayers->clear();
    _listPlayers->addItems(_listPlayerNames);
}

void Lobby::removeClient(const QString & name)
{   
    _listPlayerNames.removeAll(name);

    _listPlayers->clear();
    _listPlayers->addItems(_listPlayerNames);
}

void Lobby::setReseau(Reseau * reseau)
{
    _reseau = reseau;
}

void Lobby::sendMessage()
{
    if (!_message->text().isEmpty())
    {
        QString message = "Server : " + _message->text();

        _reseau->sendAll(Mid::LOBBYSAID, message);

        _listMessages->append(message);

        _message->clear();
    }
}

void Lobby::setWhite(quint8 id, const QString & white)
{

    for (int i = 0; i < _listGames->invisibleRootItem()->childCount(); ++i)
        if (_listGames->invisibleRootItem()->child(i)->text(0).contains(QString::number(id)))
        {
            QTreeWidgetItem * theChosenOne = _listGames->invisibleRootItem()->takeChild(i);
            theChosenOne->setText(4, white);
            _listGames->invisibleRootItem()->insertChild(i, theChosenOne);
            return;
        }
}

void Lobby::kick(QListWidgetItem * item)
{
    QString name(item->text());

    if (QMessageBox::information(this, "Confirmation", QString("Are you sure you want to kick " + item->text() + "?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
        emit kick(item->text());
}

const QStringList & Lobby::nameList() const
{
    return _listPlayerNames;
}

void Lobby::addMessage(const QString & message)
{
    _listMessages->append(message);
}

void Lobby::addGame(quint8 id, quint8 goban, qreal komi, const QString & black)
{
    QStringList list;

    list.append(QString("id=" + QString::number(id)));
    list.append(QString(QString::number(goban) + "x" + QString::number(goban)));
    list.append(QString::number(komi));
    list.append(black);

    _listGames->insertTopLevelItem(0, new QTreeWidgetItem(list));
}

void Lobby::removeGame(quint8 id)
{
    QList<QTreeWidgetItem *> list = _listGames->findItems(QString("id=" + QString::number(id)), Qt::MatchExactly);

    for (int i = 0; i < list.size(); ++i)
        _listGames->invisibleRootItem()->takeChild(_listGames->invisibleRootItem()->indexOfChild(list[i]));
}

void Lobby::showDocks()
{
    _dockChat->show();
    _dockGames->show();
    _dockPlayers->show();
}

void Lobby::hideDocks()
{
    _dockChat->hide();
    _dockGames->hide();
    _dockPlayers->hide();
}

Reseau * Lobby::reseau() const
{
    return _reseau;
}
